package com.scc.controller;
import com.scc.aop.EmpLog;
import com.scc.pojo.*;
import com.scc.service.UserService;
import com.scc.utils.AliOSSUtil;
import com.scc.utils.JwtUtil;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/emp")
public class UserController {
    //注入员工server接口
    @Autowired
    private UserService userService;

    //注入文件上传工具
    @Autowired
    private AliOSSUtil aliOSSUtil;

    ///员工登录
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp, HttpSession httpSession) {
        //日志记录
        log.info("员工登录:{}",emp);
        //连接server返回一个新的员工对象
        Emp EMP = userService.login(emp);
        //如果员工不为null下发jwt令牌
        if(EMP != null){
            //将信息储存到map集合当中
            Map<String,Object> map = new HashMap<>();
            map.put("id",EMP.getId());
            map.put("name",EMP.getName());
            map.put("account",EMP.getAccount());
            map.put("password",EMP.getPassword());
            //调用jwt工具类生成jwt
            String jwt = JwtUtil.generateJwt(map);
            //设置id
            httpSession.setAttribute("id",EMP.getId());
            //返回前端jwt令牌
            return Result.success(jwt);
        }
        //搜索员工为null,就返回错误信息
        return Result.error("登录失败，账号或密码错误");
    }

    ///员工下线记录信息
    @PostMapping("logout")
    public Result logout(@RequestBody String empOnlineMsg) {
        log.info("员工本次上线情况:{}",empOnlineMsg);
        //添加下线时间根据账号
        userService.insertEmpOnlineMsg(empOnlineMsg);
        return Result.success();
    }

    ///员工登录成功后根据账号获取头像的信息
    @GetMapping("/{account}")
    public Result getEmpImageByAccount(@PathVariable String account) {
        log.info("获取信息的用户:{}",account);
        //返回一个图片的路径
        Emp emp = userService.getEmpImageByAccount(account);
        //返回一个路径
        return Result.success(emp);
    }

    ///员工修改头像根据用户的账号
    @EmpLog
    @PostMapping("/upload/{account}")
    public Result upload(MultipartFile file,@PathVariable String account) throws IOException {
        log.info("上传照片的名称:{}",file.getOriginalFilename());
        //图片上传阿里云
        String url = aliOSSUtil.upload(file);
        log.info("文件名称:{}",url);
        //修改成功之后还要对员工信息表进行修改根据账号
        userService.modifyEmpImage(url,account);
        //返回一个新的照片路径
        return Result.success(url);
    }

    ///员工上传文件
    @EmpLog
    @PostMapping("/uploadFile")
    public Result upload(MultipartFile file) throws IOException {
        log.info("上传文件的名称:{}",file.getOriginalFilename());
        //上传阿里云
        String url = aliOSSUtil.upload(file);
        log.info("文件路径:{}",url);
        return Result.success(url);
    }

    ///获取或者查询员工的考勤信息根据员工账号
    @EmpLog
    @GetMapping("/attendance")
    public Result getEmpAttendanceMsgByAccount(@RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "10") Integer pageSize,
                                               String account, Integer empAttendanceStatus,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        log.info("员工获取或查询考勤表的信息");
        //返回一个分页对象
        PageBean pageBean = userService.getEmpAttendanceMsgByAccount(page,pageSize,account,empAttendanceStatus,startDate,endDate);
        return Result.success(pageBean);
    }

    ///修改员工的考勤状态
    @EmpLog
    @PutMapping("/attendance")
    public Result modifyEmpAttendanceStatus(@RequestBody Attendance attendance) {
        //根据id修改当前的id对应员工的考勤状态
        userService.modifyEmpAttendanceStatus(attendance);
        return Result.success();
    }

    ///员工请假提交申请表单(添加操作)
    @EmpLog
    @PostMapping("/leave")
    public Result empApplyLeave(@RequestBody Leave leave) {
        log.info("员工请假表单信息:{}",leave);
        //无需返回值
        userService.empApplyLeave(leave);
        //无需返回数据
        return Result.success();
    }

    ///员工请假信息表的获取
    @EmpLog
    @GetMapping("/leave")
    public Result getEmpApplyLeave(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                String account, Integer approveStatus,String leaveType,
                                @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate startDate,
                                @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate endDate) {
        log.info("员工请假信息表的获取:{}",account);
        //返回一个分页对象
        PageBean pageBean = userService.getEmpApplyLeave(page,pageSize,account,approveStatus,leaveType,startDate,endDate);
        return Result.success(pageBean);
    }

    ///员工个人修改信息根据id
    @EmpLog
    @PutMapping("/{id}")
    public Result empApply(@RequestBody Emp emp, @PathVariable Integer id) {
        log.info("修改信息的员工ID:{}",id);
        log.info("修改的信息是:{}",emp);
        userService.modifyEmpMegByAccount(emp,id);
        //无需返回数据
        return Result.success();
    }

    ///员工上传离职申请文件
    @EmpLog
    @PostMapping("/upload/file")
    public Result uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("上传离职申请文件的名称:{}",file.getOriginalFilename());
        String fileUrl = aliOSSUtil.upload(file);
        //文件上传的后路径
        log.info("文件上传的路径:{}",fileUrl);
        return Result.success(fileUrl);
    }

    ///员工离职申请
    @EmpLog
    @PostMapping("/resign")
    public Result empResignApply(@RequestBody Resign empResign) {
        log.info("申请离职的员工信息:{}",empResign);
        userService.empResignApply(empResign);
        return Result.success();
    }

    ///员工离职申请表信息的获取或者查询
    @EmpLog
    @GetMapping("/resign")
    public Result getEmpResignMsgByAccount(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "10") Integer pageSize,
                                           String account,Integer approveStatus,
                                           @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate startDate,
                                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        log.info(":{}员工离职信息的查询或获取",account);
        PageBean pageBean = userService.getEmpResignMsgByAccount(page,pageSize,account,approveStatus,startDate,endDate);
        return Result.success(pageBean);
    }

    ///员工获取或查询培训活动信息
    @EmpLog
    @GetMapping("/trainingActivity")
    public Result getTrainingActivityMsg(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pageSize,
                                         String account,String trainingName,Integer activityStatus,
                                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
                                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info(":{}获取查询培训活动信息",account);
        PageBean pageBean = userService.getTrainingActivityMsg(page,pageSize,account,trainingName,activityStatus,start,end);
        return Result.success(pageBean);

    }

    ///员工培训活动的报名
    @EmpLog
    @PostMapping("/trainingActivity")
    public Result empRegistrationTrainingActivity(@RequestBody String empTrainingActivity) {
        log.info("员工培训活动的报名信息:{}",empTrainingActivity);
        userService.empRegistrationActivity(empTrainingActivity);
        return Result.success();
    }

    ///获取员工培训活动表该员工的信息根据id
    @EmpLog
    @GetMapping("/trainingActivity/{id}")
    public Result getEmpTrainingActivityMsg(@PathVariable Integer id,String account,String trainingName) {
        log.info(":{}获取",account);
        log.info("获取的培训活动名称:{}",trainingName);
        //返回一个员工培训活动报名表对象
        EmpTrainingActivity empTrainingActivity = userService.getEmpTrainingActivityMsg(id,account,trainingName);
        return Result.success(empTrainingActivity);
    }

    ///员工对培训活动的取消报名操作(删除操作)
    @EmpLog
    @DeleteMapping("/trainingActivity")
    public Result empDeleteTrainingActivity(Integer id,String account) {
        log.info("员工账号:{}",account);
        log.info("培训活动的id:{}",id);
        //对员工活动报名表的删除根据id,账号
        userService.empDeleteTrainingActivityMsg(id,account);
        return Result.success();
    }

    ///员工修改密码
    @EmpLog
    @PutMapping()
    public Result modifyPassword(@RequestBody Emp emp) {
        log.info("员工修改密码信息:{}",emp);
        userService.modifyPassword(emp);
        return Result.success();
    }

    ///员工获取或者查找团建活动信息表
    @EmpLog
    @GetMapping("/teamBuildingActivities")
    public Result getTeamBuildingActivities(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer pageSize,
                                            Integer activityStatus,String teamBuildingName,String account,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("员工团建活动表的获取");
        PageBean pageBean = userService.getTeamBuildingActivities(page,pageSize,teamBuildingName,activityStatus,start,end,account);
        return Result.success(pageBean);
    }

    ///员工报名团建活动
    @EmpLog
    @PostMapping("/teamBuildingActivity")
    public Result empRegistrationTeamBuildingActivity(@RequestBody String teamBuildingActivity) {
        log.info("员工报名团建活动的信息:{}",teamBuildingActivity);
        userService.empRegistrationTeamBuildingAct(teamBuildingActivity);
        return Result.success();
    }

    ///员工获取某个团建活动的详细信息
    @EmpLog
    @GetMapping("/teamBuildingActivity")
    public Result empTeamBuildingActivityMsg(Integer id,String account) {
        //获取一个员工团建活动信息对象
        EmpTeamBuildingActivityRegistration registration = userService.empTeamBuildingActivityMsg(id,account);
        //返回前端一个对象
        return Result.success(registration);
    }

    ///员工取消报名团建活动
    @EmpLog
    @DeleteMapping("/teamBuildingActivity")
    public Result empDeleteTeamBuildingActivity(Integer id,String account) {
        userService.empDeleteTeamBuildingActivity(id,account);
        return Result.success();
    }

    ///员工获取自己的工资信息
    @EmpLog
    @GetMapping("/salary/{account}")
    public Result getSalaryMsg(@PathVariable String account,@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        log.info(":{}员工获取工资信息",account);
        PageBean pageBean = userService.getEmpSalaryByAccount(account,page,pageSize,startDate,endDate);
        return Result.success(pageBean);
    }

    ///员工根据账号搜索好友
    @EmpLog
    @GetMapping("/friend/{account}")
    public Result searchFriendByAccount(@PathVariable String account) {
        log.info("员工查询好友:{}",account);
        //返回一个员工对象集合
        List<Emp> empList = userService.searchFriendByAccount(account);
        return Result.success(empList);
    }

    ///申请好友
    @EmpLog
    @PostMapping("/friend")
    public Result addFriend(@RequestBody String applyFriendMsg) {
        log.info("申请好友信息:{}",applyFriendMsg);
        userService.insertApplyFriendMsg(applyFriendMsg);
        return Result.success();
    }

    ///查询是否有人要加我的好友
    @GetMapping("/friend/receive/{id}")
    public Result getReceiveMsg(@PathVariable Integer id) {
        log.info("接收方的id:{}",id);
        //返回一个员工表的集合
        List<Emp> empList = userService.selectFriendApply(id);
        return Result.success(empList);
    }

    ///查询我的好友申请
    @GetMapping("/friend/apply/{id}")
    public Result getApplyMsg(@PathVariable Integer id) {
        log.info("我的好友申请的id:{}",id);
        //返回一个员工表的集合
        List<Emp> applyEmpList = userService.selectApply(id);
        return Result.success(applyEmpList);
    }

    ///同意好友申请
    @PutMapping("/friend/agree")
    public Result agreeApply(@RequestBody String applyMsg) {
        log.info("好友申请信息:{}",applyMsg);
        userService.insertFriendRelationShip(applyMsg);
        return Result.success();
    }

    ///拒绝好友生申请
    @PutMapping("/friend/refuse")
    public Result refuseApply(@RequestBody String applyMsg) {
        log.info("拒绝好友申请信息:{}",applyMsg);
        userService.refuseApply(applyMsg);
        return Result.success();
    }

    ///员工获取好友列表
    @GetMapping("/friend/list/{id}")
    public Result getFriendList(@PathVariable Integer id) {
        log.info("获取好友信息:{}",id);
        List<Emp> empList = userService.selectFriendMsg(id);
        return Result.success(empList);
    }

    ///员工删除好友
    @DeleteMapping("/friend/list")
    public Result deleteFriendList(@Param("applyId") Integer applyId,@Param("friendId") Integer friendId) {
        log.info("申请删除好友的员工id:{}",applyId);
        log.info("被删除员工的id:{}",friendId);
        userService.deleteFriendListById(applyId,friendId);
        return Result.success();
    }
}
